package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.aliyun.oss.OSS;
import com.example.demo.configuration.AliyunOssConfig;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.entity.SysUser;
import com.example.demo.mapper.entity.User;
import com.example.demo.service.UserCommonService;
import com.example.demo.utils.MD5Util;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;


@RequestMapping("personController")
@Api("个人中心接口")
@Slf4j
@RestController
public class UserCommonController {

    @Autowired
    private UserCommonService service;

    private OSS ossClient;

    @ApiOperation(value = "上传頭像")
    @PostMapping("/uploadAvatar")
    public Result uploadBareheaded(MultipartFile file) throws IOException {

        log.info(file.toString());

        String originalFileName = file.getOriginalFilename();//从上传的文件中获取原始文件名
        int i = originalFileName.lastIndexOf(".");
        String png = originalFileName.substring(i);//获取文件扩展名

        //存储空间的名字
        String bucketName = "seedling-classmate";

        //创建存储对象
        ossClient = AliyunOssConfig.ossClient();

        //设置文件名并上传到OSS
        String fileName = "userAvatar/" + UUID.randomUUID().toString().substring(0,24)+png;
        ossClient.putObject(bucketName, fileName, file.getInputStream());   //之后在数据库中保存fileName
        System.out.println("当前登录账号id为：" + StpUtil.getLoginIdDefaultNull());
        if (StpUtil.getLoginIdDefaultNull() != null) {
            // 获取当前会话账号id
            System.out.println("当前登录账号id为：" + StpUtil.getLoginIdDefaultNull());

            int userId = StpUtil.getLoginIdAsInt();
            SysUser currentUser = service.selectUserById(userId);
            currentUser.setAvatar(fileName);
            service.placeAvater(currentUser);
            ossClient.shutdown();

            String result="http://seedling-classmate.oss-cn-beijing.aliyuncs.com/"+fileName;
            return Result.ok("上传成功",result);
        } else {
            ossClient.shutdown();
            return Result.warn("上传失败");
        }

    }

    @ApiOperation(value = "修改密码")
    @RequestMapping(value ="changePassword", method = {RequestMethod.POST})
    public SaResult resetPassword(@RequestBody SysUser changePwd) {

        if (StpUtil.getLoginIdDefaultNull() != null) {
            // 获取当前会话账号id, 如果未登录，则返回 null
            int userId = StpUtil.getLoginIdAsInt();
            SysUser currentUser = service.selectUserById(userId);
            //修改密码
            currentUser.setPassword(MD5Util.encryptPassword(changePwd.getPassword()));
            currentUser.setState(1);
            service.updateSysUser(currentUser);
            return SaResult.data(currentUser);
        } else {
            return SaResult.error("未登录");
        }
    }

    @ApiOperation(value = "修改个人信息")
    @RequestMapping(value ="changeUserInfo", method = {RequestMethod.POST})
    public SaResult changeUserInfo(@RequestBody SysUser userInfo) {

        if (StpUtil.getLoginIdDefaultNull() != null) {
            // 获取当前会话账号id, 如果未登录，则返回 null
            int userId = StpUtil.getLoginIdAsInt();
            SysUser currentUser = service.selectUserById(userId);
            //修改密码
            currentUser.setName(userInfo.getName());
            currentUser.setPhone(userInfo.getPhone());
            currentUser.setEmail(userInfo.getEmail());
            //System.out.println("user = " + user);
            service.updateSysUser(currentUser);
            return SaResult.data(currentUser);
        } else {
            return SaResult.error("未登录");
        }
    }
}


